package com.paad.todolist;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

import java.util.*;
import android.view.*;
import android.widget.*;

public class TileBattlegridAdapter extends ArrayAdapter<Tile> {

  int resource_id;
  static final int numcols = 7;
  Context con;

  public TileBattlegridAdapter(Context _context, 
                             int _resource_id, 
                             List<Tile> _items) {
    super(_context, _resource_id, _items);
    resource_id = _resource_id;
    con = _context;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
	  View v = convertView;
      if (v == null) {
          LayoutInflater vi = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.squad_battlegrid, null);
      }

    Tile t =   getItem(position);
    Squad sq = t.sq;
    TextView  text  = (TextView)  v.findViewById(R.id.battlegridSquadText);
    ImageView image = (ImageView) v.findViewById(R.id.battlegridSquadImage);
    
    if(sq != null)
    {
    	
    	if(text != null)
    	{
    		//text.setText(sq.squadType + " x" + sq.currentUnits);
    		text.setText("" + sq.currentUnits);
    	}
    	
    	if( image != null)
    	{
    	
    		image.setImageResource(R.drawable.icon);
    		switch(sq.img)
    		{
    		case swordsmen:
    			if(sq.team)
    				image.setImageResource(R.drawable.swordsmen_tile_big);
    			else 
    				image.setImageResource(R.drawable.swordsmen_tile_big_red);
    			break;
    		case archers:
    			if(sq.team)
    				image.setImageResource(R.drawable.archer_tile_big);
    			else 
    				image.setImageResource(R.drawable.archer_tile_big_red);
    			break;
    		case cavalry:
    			if(sq.team)
    				image.setImageResource(R.drawable.cavavalry_tile_big);
    			else 
    				image.setImageResource(R.drawable.cavavalry_tile_big_red);
    			break;
    		default:
    			image.setImageResource(R.drawable.icon);
    		}
    		
    		
    		
    		image.setMinimumHeight(parent.getWidth()/numcols);
    		image.setMinimumWidth (parent.getWidth()/numcols);
    		//image.setMaxHeight(v.getWidth());
    		//image.setMaxWidth (v.getWidth());
    	}
    	
    	//if we are an attack target, then display the overlay.
    	if(t.validAttackTarget)
    	{
	    	Resources r = con.getResources(); 
	    	Drawable[] layers = new Drawable[2];
	    	layers[0] = image.getDrawable();
	    	layers[1] = r.getDrawable(R.drawable.target_tile_alt_big);
	    	LayerDrawable layerDrawable = new LayerDrawable(layers);
	    	image.setImageDrawable(layerDrawable);
    	} else if (sq.hasActed)
    	{
    		Resources r = con.getResources(); 
	    	Drawable[] layers = new Drawable[2];
	    	layers[0] = image.getDrawable();
	    	layers[1] = r.getDrawable(R.drawable.unit_done_big);
	    	LayerDrawable layerDrawable = new LayerDrawable(layers);
	    	image.setImageDrawable(layerDrawable);
    	}

    } else {
    	if(text != null)
    	{
    		text.setText("");
    	}
    	if(image != null)
    	{
    		image.setImageResource(R.drawable.empty_tile_alt_big);
    		
    		if(t.validMoveTarget)
    		{
    			Resources r = con.getResources(); 
    	    	Drawable[] layers = new Drawable[2];
    	    	layers[0] = image.getDrawable();
    	    	layers[1] = r.getDrawable(R.drawable.move_tile_big);
    	    	LayerDrawable layerDrawable = new LayerDrawable(layers);
    	    	image.setImageDrawable(layerDrawable);
    		}
    		//image.setMaxHeight(v.getWidth());
    		//image.setMaxWidth(v.getWidth());
    		image.setMinimumHeight(parent.getWidth()/numcols);
    		image.setMinimumWidth (parent.getWidth()/numcols);
    	}
    }
    

    return v;
  }
}
